﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCP.UI
{ 
    /// <summary>
    /// 原煤产量台帐
    /// </summary>
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 王堃
    /// Version: 1.0
    /// History:
    ///       2011-07-28  王堃   [创建]
    ///
    public partial class ZMCP30 : EFViewCmes
    {
        public ZMCP30()
        {
            InitializeComponent(); 
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnQueryButtonClick(object sender, RoutedEventArgs e)
        {
            Submit(OperationMode.Query, "ZMCP30", "query", DataRange.All, OnQueryCompleted);
        }

        private void OnQueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() != -1)
            {
                grid.Refresh();
                //按钮生效
                base.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "原煤产量台帐—新增页面", "insert", null, sender, e);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);
        }

        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (null != this.grid.SelectedItem)
            {
                EIRow _row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();

                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdOutputId", _row["fdKeyId"].ToString());
                this.container.LoadView("ZMCP31", args);
            }
            else
            {
                this.container.Content = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("month"));
            AddCoilOutputAccount addCoilOutputAccount = new AddCoilOutputAccount(_dialogType);//构造一个新的修改页面
            addCoilOutputAccount.EIDataContext = new Platform.Core.UI.EIDataContext();
            addCoilOutputAccount.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = addCoilOutputAccount;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 重新基类的方法，当有新的EIInfo 数据发布时回调的方法
        /// 生产进度新增修改后更新统计数据
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(EIInfo data)
        {
            if (null != this.grid.SelectedItem)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCP30");
                eiInfo.Set("method", "query");
                string fdKeyId = this.grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.grid.SelectedItem, null) as string;
                EIBlock block = eiInfo.AddBlock(EIConstants.QueryStatusBlock);

                EIColumn column = new EIColumn();
                column.Name = "fdKeyId";
                block.AddColumn(column);

                EIRow row = new EIRow();
                row.Add("fdKeyId", fdKeyId);
                block.AddRow(row);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, UpdateStatisticsData);
            }
        }

        /// <summary>
        /// 更新统计数据
        /// </summary>
        private void UpdateStatisticsData(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo == null || e.EIInfo.GetStatus() == -1)
            {
               
            }
            else
            {
                EIRow row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
                row["fdAcctualSum"] = e.EIInfo.GetBlock(EIConstants.ResultBlock).Get(0, "fdAcctualSum");
            }
        }
    }
}
